package org.fisco.bcos.evidence.controller;

import org.fisco.bcos.evidence.bean.Account;
import org.fisco.bcos.evidence.client.AccountKVClient;
import org.fisco.bcos.evidence.client.ShaTestClient;
import org.fisco.bcos.evidence.config.ResultCode;
import org.fisco.bcos.evidence.config.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/account")
public class AccountClientController {

    @Autowired
    private AccountKVClient client;


    ///添加用户
    @RequestMapping(method = {RequestMethod.POST}, value = "/addAccount")
    public ResultJson addAccount(
            @RequestParam(value = "id", required = true) String id,
            @RequestParam(value = "identity") String identity,
            @RequestParam(value = "name") String name,
            @RequestParam(value = "accountType") String accountType) {

        Object resp = null;
        try {
            Account account = new Account();
            account.setId(id);
            account.setIdentity(identity);
            account.setName(name);
            account.setAccountType(accountType);

            resp = client.setAccount(account);
            if (resp == null) {
                return ResultJson.ok();
            }
        } catch (Exception e) {
            return ResultJson.failure(ResultCode.ExecutionException, e.toString());
        }
        return ResultJson.ok(resp, "成功");
    }

    ///查找用户
    @GetMapping("/getAccount")
    public ResultJson getAccount(
            @RequestParam(value = "id", required = true) String id) {
        Object resp = null;
        try {
            resp = client.getAccount(id);
            if (resp == null) {
                return ResultJson.ok();
            }
        } catch (Exception e) {
            return ResultJson.failure(ResultCode.ExecutionException, e.toString());
        }
        return ResultJson.ok(resp, "成功");
    }


    ///查看用户是否已经上链,如果没有上链则创建新用户
    @RequestMapping(method = {RequestMethod.POST}, value = "/selectForUpdateAccount")
    public ResultJson selectForUpdateAccount(
            @RequestParam(value = "id", required = true) String id,
            @RequestParam(value = "identity") String identity,
            @RequestParam(value = "name") String name,
            @RequestParam(value = "accountType") String accountType) {

        Object resp = null;
        try {
            Account account = client.getAccount(id);
            if (account == null) { /// create account
                account = new Account();
                account.setId(id);
                account.setIdentity(identity);
                account.setName(name);
                account.setAccountType(accountType);
                if (client.setAccount(account)) {
                    account = client.getAccount(id);
                }
            }
            resp = account;
            if (resp == null) {
                return ResultJson.ok();
            }
        } catch (Exception e) {
            return ResultJson.failure(ResultCode.ExecutionException, e.toString());
        }
        return ResultJson.ok(resp, "成功");
    }


    ////////////////////////////////////////////////////////////////////////
    ////////////////////////////////TEST////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////


    ///1. (test) set
    @GetMapping("/set")
    public boolean set() throws Exception {
//        if (!initFlag) init();

        Account account = new Account();
        account.setId("0x99100");
        account.setIdentity("1010101010101010");
        account.setName("Jone");
        account.setAccountType("Person");
        account.setAccountHash("0x121212121112121212");

        return client.setAccount(account);
    }

    ///2. (test) get
    @GetMapping("/get")
    public Account get() throws Exception {
//        if (!initFlag) init();
        return client.getAccount("0x99100");
    }
}

/**
 * test:
 * http://localhost:8080/account/set
 * http://localhost:8080/account/get
 */
